package com.lsu.prototype.homework;

import java.io.*;

/**
 * 人
 *
 * @Author wang suo
 * @Date 2020/11/30 0030 14:22
 * @Version 1.0
 */
public class Person implements Serializable, Prototype {
    /**
     * 坐骑
     */
    private Horse mount;

    Person(Horse horse) {
        super();
        this.mount = horse;
    }

    public Horse getMount() {
        return mount;
    }

    @Override
    public Object cloneSelf() {
        Object obj = null;
        try {
            //obj = clone();
            // 使用深克隆
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(this);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            obj = objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * 文件存档
     */
    void archive(String path, String fileName) {
        File file = new File(path, fileName);
        boolean b = true;
        ObjectOutputStream objectOutputStream = null;
        try {
            if (!file.exists()) {
                b = file.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (b && file.exists()) {
            // 写入到文件中
            try {
                objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
                objectOutputStream.writeObject(this);
                System.out.println("文件序列化成功，本地地址："+ path + "文件名：" + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭输出流
                try {
                    if (objectOutputStream != null) {
                        objectOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 读取存档
     */
    Person readArchive(String path, String fileName) {
        File file = new File(path, fileName);
        ObjectInputStream objectInputStream = null;
        Person obj = null;
        // 写入
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(file));
            obj = (Person) objectInputStream.readObject();
            System.out.println("文件读取成功");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 关闭输入流
            try {
                if (objectInputStream != null) {
                    objectInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    /**
     * 人跑路
     */
    void run() {
        // 使用坐骑跑路
        mount.run();
        // 显示路程
        mount.show();
    }
}